<?php

namespace Home\Model;

use Think\Model;
use Home\Model\ArticleModel;

class TagModel extends Model {
	
	/**
	 * 搜索标签
	 *
	 * @param unknown $name        	
	 * @return string
	 */
	public function search($name) {
		$page = intval ( $_REQUEST ['page'] ) < 1 ? 1 : $_REQUEST ['page'];
		$num = C ( 'page_num' );
		$where = "name='{$name}'";
		$order = 'obj_type desc,id desc';
		
		// 读取缓存
		$cacheName =   ( $_GET ['mobile'].'tag_search_' . $where . $page . $order . $num );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}
		$result ['list'] = $this->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		foreach ( $result ['list'] as $k => $v ) {
			if ($v ['obj_type'] == 'site') {
				$site = new SiteModel ();
				$result ['list'] [$k] = $site->getInfo ( $v ['obj_id'] );
			} elseif ($v ['obj_type'] == 'article') {
				$article = new ArticleModel ();
				$result ['list'] [$k] = $article->getInfo ( $v ['obj_id'] );
			}
		}
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['page'] = page_html ( $result ['total_num'], $page, $num );
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		if ($page > 1) {
			$result ['prev_page'] = $page - 1;
		} else {
			$result ['prev_page'] = '';
		}
		
		// 获取关键词介绍
		$site = new SiteModel ();
		$siteInfo = $site->where ( 'name="' . $name . '"' )->find ();
		$result ['siteIntro'] = $siteInfo ['intro'];
		S ( $cacheName, $result, C ( 'cache_time_short' ) );
		return $result;
	}
	
	/**
	 * 获取热门标签
	 *
	 * @param number $num        	
	 */
	public function getHotTag($num = 24) {
		/*
		 * $sql = "SELECT `id`,count(`name`) as num FROM " . C ( 'DB_PREFIX' ) . "tag GROUP BY `name` ORDER BY num desc LIMIT " . intval ( $num ); $result ['list'] = $this->query ( $sql ); foreach ( $result ['list'] as $k => $v ) { $result ['list'] [$k] = $this->getInfo ( $v ['id'] ); }
		 */
		$data [] = array (
				'name' => '发财树',
				'url' => 'http://hua.fengqiaoju.com/muben/facaishu/'
		);
		$data [] = array (
				'name' => '康乃馨',
				'url' => 'http://hua.fengqiaoju.com/duoniansheng/kangnaixin/'
		);
		$data [] = array (
				'name' => '风信子',
				'url' => 'http://hua.fengqiaoju.com/qiugen/fengxinzi/'
		);
		$data [] = array (
				'name' => '吊兰',
				'url' => 'http://hua.fengqiaoju.com/duoniansheng/diaolan/'
		);
		$data [] = array (
				'name' => '玫瑰',
				'url' => 'http://hua.fengqiaoju.com/muben/meigui/'
		);
		$data [] = array (
				'name' => '富贵竹',
				'url' => 'http://hua.fengqiaoju.com/duoniansheng/fuguizhu/'
		);
		$data [] = array (
				'name' => '绿萝',
				'url' => 'http://hua.fengqiaoju.com/yierniansheng/lvluo/'
		);
		$data [] = array (
				'name' => '百合',
				'url' => 'http://hua.fengqiaoju.com/qiugen/baihe/'
		);
		$data [] = array (
				'name' => '君子兰',
				'url' => 'http://hua.fengqiaoju.com/duoniansheng/junzilan/'
		);		
		$data [] = array (
				'name' => '多肉植物',
				'url' => 'http://hua.fengqiaoju.com/tag/多肉植物/' 
		);
		$data [] = array (
				'name' => '观花植物',
				'url' => 'http://hua.fengqiaoju.com/tag/观花植物/' 
		);
		$data [] = array (
				'name' => '观叶植物',
				'url' => 'http://hua.fengqiaoju.com/tag/观叶植物/' 
		);
		$data [] = array (
				'name' => '草本植物',
				'url' => 'http://hua.fengqiaoju.com/tag/草本植物/' 
		);
		$data [] = array (
				'name' => '木本植物',
				'url' => 'http://hua.fengqiaoju.com/tag/木本植物/' 
		);
		$data [] = array (
				'name' => '水生植物',
				'url' => 'http://hua.fengqiaoju.com/tag/水生植物/' 
		);
		$data [] = array (
				'name' => '室内植物',
				'url' => 'http://hua.fengqiaoju.com/tag/室内植物/' 
		);
		$data [] = array (
				'name' => '水培植物',
				'url' => 'http://hua.fengqiaoju.com/tag/水培植物/' 
		);
		$data [] = array (
				'name' => '食肉植物',
				'url' => 'http://hua.fengqiaoju.com/tag/食肉植物/' 
		);
		$data [] = array (
				'name' => '花卉养殖',
				'url' => 'http://hua.fengqiaoju.com/tag/花卉养殖/' 
		);
		$data [] = array (
				'name' => '办公室花卉',
				'url' => 'http://hua.fengqiaoju.com/tag/办公室花卉/' 
		);
		$data [] = array (
				'name' => '花卉盆景',
				'url' => 'http://hua.fengqiaoju.com/tag/花卉盆景/' 
		);
		$data [] = array (
				'name' => '花卉图片',
				'url' => 'http://hua.fengqiaoju.com/tag/花卉图片/'
		);		
		$data [] = array (
				'name' => '花卉市场',
				'url' => 'http://hua.fengqiaoju.com/tag/花卉市场/'
		);
		$data [] = array (
				'name' => '家庭养花',
				'url' => 'http://hua.fengqiaoju.com/tag/家庭养花/'
		);

		
		$result ['list'] = $data;
		return $result;
	}
	
	/**
	 * 获取单条信息
	 *
	 * @param unknown $id        	
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, mixed, unknown, string, object>
	 */
	public function getInfo($id) {
		$where = 'id=' . intval ( $id );
		$result = $this->where ( $where )->find ();
		$result ['url'] = C ( 'site_url' ) . "/tag/" . $result ['name'] . '/';
		return $result;
	}
}
